package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.example.studyAndroid.R;
import domain.LeftDomain;
import ui.GridAdapter;
import ui.MainLeftAdapter;
import utils.Config;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shaxj on 2015/5/22.
 */
public class GridViewActivity extends Activity {

    // gridTitle
    List<String> gridDataList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);
        init();
    }

    public void init() {
        intLeft();
        initSpinner();
        initMainGrid();
    }

    public void initSpinner() {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        List<String> yearList = new ArrayList<String>();
        yearList.add(" ");
        yearList.add(" 2012");
        yearList.add(" 2013");
        yearList.add(" 2014");
        yearList.add(" 2015");
        ArrayAdapter spinnerAdapter = new ArrayAdapter(this, R.layout.myspinner, yearList);
        spinner.setAdapter(spinnerAdapter);
    }

    public void initMainGrid() {
        // gridTitle
        gridDataList = new ArrayList<String>();
        gridDataList.add("title1");
        gridDataList.add("title2");
        gridDataList.add("title3");
        gridDataList.add("title4");
        gridDataList.add("title5");
        gridDataList.add("title6");
        gridDataList.add("title7");
        // grid
        for (int i = 0; i < 30; i++) {
            gridDataList.add("_" + i + "_");
        }
        GridAdapter grid = new GridAdapter(this, gridDataList);
        GridView gridTitleView = (GridView) findViewById(R.id.gv_main);
        gridTitleView.setAdapter(grid);
        gridTitleView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity.this, gridDataList.get(position), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(GridViewActivity.this, IndexActivity.class);
                intent.putExtra("data", gridDataList.get(position));
                startActivity(intent);
            }
        });

    }

    /**
     * 左侧抽屉信息栏
     */
    public void intLeft() {

        // 数据行用户名和退出按钮
        List<LeftDomain> listDomain = new ArrayList<LeftDomain>();
        listDomain.add(new LeftDomain(R.drawable.user, MyApplication.getInstance().sharedPreferences.getString(Config.USER_NAME, ""), ""));
        listDomain.add(new LeftDomain(R.drawable.quit, "退出", ""));

        ListView leftListView = (ListView) findViewById(R.id.left_drawer_list);
        leftListView.setAdapter(new MainLeftAdapter(this, listDomain));

        // 点击事件
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity.this, position + " ", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
